//
//  XS_Label.swift
//  WorkDemo
//
//  Created by 韩云智 on 2019/9/30.
//  Copyright © 2019 韩云智. All rights reserved.
//

import Foundation
import UIKit

public extension UILabel {
    static var defaultColor: UIColor = .black
    /// 工厂模式生成Label
    /// - Parameter size: 字体大小, 默认16
    /// - Parameter name: 字体名称, 默认PingFangSC-Regular
    /// - Parameter color: 文字颜色, 默认 defaultColor
    /// - Parameter alignment: 文字对齐, 默认left
    /// - Parameter lines: 文字行数, 默认1
    /// - Parameter text: 文本内容
    static func xsLabel(size: CGFloat = 16, fontName: UIFont.XS_FontName = .regular, color: UIColor = defaultColor, alignment: NSTextAlignment = .left, lines: Int = 1, text: String? = nil) -> Self {
         return xsLabel(font: .xsFont(name: fontName, size), color: color, alignment: alignment, lines: lines, text: text)
    }
    static func xsLabel(size: CGFloat = 16, name: String, color: UIColor = defaultColor, alignment: NSTextAlignment = .left, lines: Int = 1, text: String? = nil) -> Self {
        return xsLabel(font: .xsNFont(name: name, size), color: color, alignment: alignment, lines: lines, text: text)
    }
    static func xsLabel(font: UIFont, color: UIColor = defaultColor, alignment: NSTextAlignment = .left, lines: Int = 1, text: String? = nil) -> Self {
        let label = self.init()
        label.font = font
        label.textColor = color
        label.textAlignment = alignment
        label.numberOfLines = lines
        label.text = text
        return label
    }
}

open class XS_LetterLabel: UILabel {
    
    open lazy var textShadowColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
    open lazy var textShadowWidth: CGFloat = 2
    
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    open override func draw(_ rect: CGRect) {
        // Drawing code
        let shadowOffset = self.shadowOffset
        let textColor = self.textColor
        
        let c = UIGraphicsGetCurrentContext()
        c!.setLineWidth(textShadowWidth)
        c!.setLineJoin(.round)
        c!.setTextDrawingMode(.stroke)
        self.textColor = textShadowColor
        super.drawText(in: rect)
        
        c!.setTextDrawingMode(.fill)
        self.textColor = textColor
        self.shadowOffset = CGSize(width: 0, height:0)
        super.drawText(in: rect)
        self.shadowOffset = shadowOffset
        
        layer.masksToBounds = false
    }
}
